package com.dai.spec.gxk.controller;

import com.dai.spec.common.annotation.Log;
import com.dai.spec.common.core.controller.BaseController;
import com.dai.spec.common.core.page.TableDataInfo;
import com.dai.spec.common.enums.*;
import com.dai.spec.common.utils.poi.ExcelUtil;
import com.dai.spec.gxk.request.MyCoursePageRequest;
import com.dai.spec.gxk.service.MyCourseService;
import com.dai.spec.gxk.vo.MyCourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 *教师课程信息管理Controller
 * 老师接口，管理选了该老师所任教的课程
 * @author daikl
 * @date 2024-02-29
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/gxk/myCourse")
@Api("教师的课程信息管理")
public class MyCourseController extends BaseController {
    private MyCourseService myCourseService;

    public MyCourseController(MyCourseService myCourseService) {
        this.myCourseService = myCourseService;
    }
    /**
     * 查询我任教的课程列表
     */
    @ApiOperation("查询我任教的课程列表")
    @PreAuthorize("@ss.hasPermi('gxk:myCourse:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCoursePageRequest pageRequest) {

        List<MyCourseVO> list = myCourseService.queryMyCourseList(pageRequest);
        return getDataTable(list);
    }

    /**
     * 导出课程信息管理列表
     */
    @ApiOperation("导出我任教课程信息列表")
    @PreAuthorize("@ss.hasPermi('gxk:myCourse:export')")
    @Log(title = "课程信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyCoursePageRequest pageRequest) {
        List<MyCourseVO> myCourseVOList = myCourseService.queryList(pageRequest);
        List<MyCourseVO> courseVOList = myCourseVOList.stream().map(item -> {
            MyCourseVO myCourseVO = coverCourse(item);
            return myCourseVO;
        }).collect(Collectors.toList());
        ExcelUtil<MyCourseVO> util = new ExcelUtil<>(MyCourseVO.class);
        util.exportExcel(response, courseVOList, "任教的课程信息管理数据");
    }

    private static MyCourseVO coverCourse(MyCourseVO item) {
        String department = item.getDepartment();
        item.setDepartment(DepartmentType.getValueByCode(department));

        String courseCycle = item.getCourseCycle();
        item.setCourseCycle(CourseCycleType.getValueByCode(courseCycle));

        String type = item.getType();
        item.setType(CourseType.getValueByCode(type));

        String status = item.getStatus();
        item.setStatus(CourseStatus.getValueByCode(status));

        String deliveryType = item.getDeliveryType();
        item.setDeliveryType(DeliveryType.getValueByCode(deliveryType));
        return item;
    }

}
